package com.tywj.panda.launch.api

import com.tywj.panda.customer.common.constant.ThirdPartyAccount
import com.tywj.panda.customer.common.manager.AuthorityManager
import com.tywj.panda.customer.common.tool.HttpTool
import com.tywj.lib.net.retrofit.request.GetRequest
import com.tywj.lib.net.retrofit.request.PostRequest

object LaunchApi {
    //接口测试
    val LOAD_BANNER_URL = "IpandaApi/banner/getBannerByType"
    /**
     * 微信登录官方文档
     * https://open.weixin.qq.com/cgi-bin/showdocument?action=dir_list&t=resource/res_list&verify=1&id=open1419317853&token=&lang=zh_CN
     */
    //微信登录
    private const val WX_LOGIN = "https://api.weixin.qq.com/sns/oauth2/access_token?"

    //获取微信用户信息
    private const val WX_GET_INFO = "https://api.weixin.qq.com/sns/userinfo"

    // 微信中access_token过期后，请求以下链接刷新access_token
    private const val REFRESH_ACCESS_TOKEN_URL_WX = "https://api.weixin.qq.com/sns/oauth2/refresh_token?"

    private const val REFRESH_AUTH = "user/refresh-auth"

    private const val LOGOUT = "user/login-out"

    /**
     * 获取验证码
     */
    private const val GET_CODE = "user/auth/get/validate"

    /**
     * App登录接口(一键登录)
     */
    val LAUNCH_LOGIN = "user/auth/login"


    /**
     *
     */
    fun loadBanner(type: Int): PostRequest {
        return HttpTool.post(LOAD_BANNER_URL, false)
            .param("type", type)

    }

    /**
     * 登录
     * 默认token 有效期1天，过期则需刷新token，如果半个月未打开应用，则需重新登录授权。登录请求接口,
     * 不同登录方式，所需字段不一，请按照需求传值.
     * @param aliAccessToken 键登录请求Token（一键登录必填 )
     * @param aliId: 支付宝 aliId （支付宝预留）
     * @param alipayToken: 支付宝 token
     * @param loginType 登录/注册类型 1:阿里一键登录,2:微信授权登录3:支付宝授权登录4:QQ授权登录5:手机验证码登录，必填。
     * @param userType 1用户登录 2 司机登录
     * @param platformType  平台类型 1:IOS,2:Android ,必填
     * @param qqAccessToken  QQ Token
     * @param qqUnionId  QQ id
     * @param unionId  微信预留
     * @param openId  微信openId
     * @param wxAccessToken  微信token
     */
    fun login(
        aliAccessToken: String = "",
        aliId: String = "",
        alipayToken: String = "",
        loginType: Int = 1,
        userType: Int = 1,
        phone: String = "",
        validateCode: String = "",
        openId: String = "",
        platformType: Int = 2,
        qqAccessToken: String = "",
        qqUnionId: String = "",
        unionId: String = "",
        wxAccessToken: String = ""
    ): PostRequest {
        return HttpTool.post(LAUNCH_LOGIN, isAuth = false)
            .param("ali_accesstoken", aliAccessToken)
            .param("alipay_id", aliId)
            .param("alipay_token", alipayToken)
            .param("login_type", loginType)
            .param("user_type", userType)
            .param("mobile_phone_number", phone)
            .param("mobile_validate_code", validateCode)
            .param("open_id", openId)
            .param("platform_type", platformType)
            .param("qq_access_token", qqAccessToken)
            .param("qq_unionid", qqUnionId)
            .param("union_id", unionId)
            .param("wx_access_token", wxAccessToken)
    }

    fun logout(): PostRequest {
        return HttpTool.post(LOGOUT).param("uid", AuthorityManager.uid)
    }

    /**
     * 获取微信授权token
     */
    fun getAccessToken(code: String): GetRequest {
        return HttpTool.get(WX_LOGIN, false)
            .param("appid", ThirdPartyAccount.WECHAT_APPID)
            .param("secret", ThirdPartyAccount.WECHAT_SECRET)
            .param("code", code)
            .param("grant_type", "authorization_code")
    }

    /**
     * 获取微信用户信息
     */

    fun getWxUserInfo(accessToken: String, openId: String): GetRequest {
        return HttpTool.get(WX_GET_INFO, false)
            .param("access_token", accessToken)
            .param("openid", openId)
    }

    //"https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=APPID&grant_type=refresh_token&refresh_token=REFRESH_TOKEN_VALUE"
    /**
     * 刷新微信refreshToken
     */
    fun getRefeshWxToken(refreshToken: String): GetRequest {
        return HttpTool.get(REFRESH_ACCESS_TOKEN_URL_WX, false)
            .param("appid", ThirdPartyAccount.WECHAT_APPID)
            .param("grant_type", "refresh_token")
            .param("refresh_token", refreshToken)
    }

    /**
     * 刷新令牌
     */
    fun refrechAuth(uid: String): PostRequest {
        return HttpTool.post(REFRESH_AUTH).param("uid", uid)
    }

    /**
     * 获取验证码
     * @param authorization
     */
    fun getValidateCode(phone: String): PostRequest {
        return HttpTool.post(GET_CODE, false)
            .param("phone", phone)
    }
}